<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px"  style="display: flex; align-items: center;">
      <el-form-item label="收货地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入收货地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人" prop="receiver">
        <el-input
          v-model="queryParams.receiver"
          placeholder="请输入收货人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

       <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

      <div style="margin-left:auto;">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:address:add']"
          :disabled="ccuscode==''"
        >新增</el-button>

        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:address:export']"
        >导出</el-button>
      </div>

    </el-form>


    <el-table v-loading="loading" :data="addressList" @selection-change="handleSelectionChange"  @row-dblclick="handleRowDblClick">
      <el-table-column type="selection" width="80" align="left" />
      <el-table-column label="" align="center" prop="id"  v-if="false"  width="0"/>
      <el-table-column label="No" align="left" type="index" width="50" />
      <el-table-column label="地区编码" align="center" prop="areacode" width="100" />
      <el-table-column label="地区名称" align="center" prop="areafullname"  width="400"/>
      <el-table-column label="地址" align="center" prop="address"  width="400" />
      <el-table-column label="电话" align="center" prop="phone"  width="200"/>
      <el-table-column label="收货人" align="center" prop="receiver"  width="150"/>
      <el-table-column label="操作" align="center"   width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:address:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:address:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改customer_address对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body  :z-index="2800">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="地区编码" prop="areacode">
              <el-input v-model="form.areacode" placeholder="地区编码" >
              <el-button slot="append" icon="el-icon-search" @click="showAreaDialog"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地区名称" prop="areafullname">
              <el-input v-model="form.areafullname" placeholder="市/地区名称"  readonly/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="收货地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="收货联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="收货人姓名" prop="receiver">
          <el-input v-model="form.receiver" placeholder="请输入收货人" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="areaDialogVisible"
      title="选择区域"
      width="400px"
      top="5vh"
      append-to-body
      :z-index="3000"
      class="area-dialog"
        >
      <Region @nodeDbclick="selectAreaFromDialog"  :areacode="areacode"     :isDialog="true" />
    </el-dialog>
  </div>
</template>

<script>
import { listAddress, getAddress, delAddress, addAddress, updateAddress, exportAddress } from "@/api/business/address";
import     Region        from  '@/views/business/region/index.vue'

export default {
  name: "CustomerAddress",
  components: {  Region  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      areacode:null,
      areaDialogVisible:false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // customer_address表格数据
      addressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 字典
      idOptions: [],
      // 字典
      addressOptions: [],
      // 字典
      phoneOptions: [],
      // 字典
      receiverOptions: [],
      // 字典
      // 字典
      customerOptions: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        address: null,
        phone: null,
        receiver: null,
        areacode: null,
        customer: null,
      },
      // 表单参数
      form: {
        areacode: null,
        areafullname: null,

      },
      // 表单校验
      rules: {
      }
    };
  },
  props:{
    ccuscode:{
      type:String,
      default:''
    } ,isDialog: {
      type: Boolean,
      default: false
    }
  },
  created() {
    this.getList();

  },
  methods: {
    /** 查询customer_address列表 */
    getList() {
      debugger;
      this.loading = true;
      this.queryParams.ccuscode=this.ccuscode;
      listAddress(this.queryParams).then(response => {
        this.addressList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    }, showAreaDialog() {
      this.areaDialogVisible = true;
      this.areacode=this.form.areacode;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    }, selectAreaFromDialog(node) {
         debugger;
         this.form.areacode=node.code;
         this.form.areafullname=node.fullname;
         console.log("form",this.form);
         this.areaDialogVisible=false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        address: null,
        phone: null,
        receiver: null,
        areacode: null,
        customer: null,
        areafullname: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },handleRowDblClick(row){
      debugger;
      if (this.isDialog) {
        this.$emit('dbclick', row);
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收货地址";
      this.form.ccuscode=this.ccuscode;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAddress(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改地址";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAddress(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAddress(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除customer_address编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAddress(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有customer_address数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAddress(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>

<!-- scoped + ::v-deep 只能影响组件内的 DOM，但 Element UI 的 el-dialog 是挂载到 body 下的，scoped 样式有时会被 hash 隔离，
 导致 ::v-deep 也不生效（尤其是 vue2 + vue-loader 旧版本） -->
<style lang="scss"   >
  .area-dialog  .el-dialog__body {
  padding: 1px !important;
}

  .area-dialog .el-dialog{
   position: absolute;
   margin-left: 50px;
 }
</style>
